package lib.common;

import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.vecmath.Point2d;

public class ColorOperation {
	public static Color blendColors(Color c1, Color c2) {
		final float clampFactor = 1.0f;
		float blend;
		float[] c1RGB, c2RGB, resultRGB = {0, 0, 0};
		
		c1RGB = c1.getColorComponents(null);
		c2RGB = c2.getColorComponents(null);
		
		for (int i = 0 ; i < c1RGB.length ; i++) {
			blend = c1RGB[i] * c2RGB[i];
			
			if (blend > clampFactor) {
				blend = 1.0f;
			}
			
			resultRGB[i] = blend;
		}
		
		return new Color(resultRGB[0], resultRGB[1], resultRGB[2]);
	}
	
	public static Color addColors(Color c1, Color c2) {
		final float clampFactor = 1.0f;
		float blend;
		float[] c1RGB, c2RGB, resultRGB = {0, 0, 0};
		
		c1RGB = c1.getColorComponents(null);
		c2RGB = c2.getColorComponents(null);
		
		for (int i = 0 ; i < c1RGB.length ; i++) {
			blend = c1RGB[i] + c2RGB[i];
			
			if (blend > clampFactor) {
				blend = 1.0f;
			}
			
			resultRGB[i] = blend;
		}
		
		return new Color(resultRGB[0], resultRGB[1], resultRGB[2]);
	}
	
	
	
	public static Color multiplyColor(Color c, double factor) {
		float[] cRGB;
		final float clampFactor = 1.0f;
		float cComponent;
		
		cRGB = c.getColorComponents(null);
		
		for (int i = 0 ; i < cRGB.length ; i++ ) {
			cComponent = cRGB[i];
			cComponent *= factor;
			
			if (cComponent > clampFactor) {
				cComponent = 1.0f;
			}
			
			cRGB[i] = cComponent;
		}
		
		return new Color(cRGB[0], cRGB[1], cRGB[2]);
	}
	
	public static Color average(Color[] list) {
		int red = 0;
		int green = 0;
		int blue = 0;
		
		for (Color c: list) {
			red += c.getRed();
			green += c.getGreen();
			blue += c.getBlue();
		}
		
		int size = list.length;
		return new Color(red/size, green/size, blue/size);
	}
	
	
	public static Color[] getImageColors( BufferedImage textureImage ) {
		
		Color [] colorArray = new Color [textureImage.getHeight()*textureImage.getHeight()];
			
			int i = 0;
			for ( int y = 0; y < textureImage.getHeight(); y++ ){
				for ( int x = 0; x < textureImage.getWidth(); x++ ){
					colorArray[i++] = new Color(textureImage.getRGB(x, y));
				}
			}
		
		return colorArray;
	}

	public static Point2d getImageResolution(String texture) {
		
		return null;
	}
}
